import constant from './constant'

// 存储变量名
const storageKey = 'storage_data'

// 存储节点变量名
const storageNodeKeys = [constant.avatar, constant.id, constant.name, constant.roles, constant.permissions]

const storage = {
  set(key, value) {
    if (storageNodeKeys.includes(key)) {
      let tmp = uni.getStorageSync(storageKey)
      tmp = tmp || {}
      tmp[key] = value
      uni.setStorageSync(storageKey, tmp)
    }
  },
  get(key) {
    const storageData = uni.getStorageSync(storageKey) || {}
    return storageData[key] || ''
  },
  remove(key) {
    const storageData = uni.getStorageSync(storageKey) || {}
    delete storageData[key]
    uni.setStorageSync(storageKey, storageData)
  },
  clean() {
    uni.removeStorageSync(storageKey)
  },
}

export default storage
